package FileIO;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-19
 * Time: 21:50
 */
public class IODemo12 {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        //把一个路径下的文件拷贝到另一个路径下面  (二进制的方式进行读取，写入)
        System.out.println("请输入你要拷贝的文件目录：");
        String SrcPath = scanner.nextLine();
        File file = new File(SrcPath);
        //判断这个文件是否存在
        if (!file.isFile()) {
            System.out.println("输入的路径可能不是一个文件!");
            return;
        }
        System.out.println("请输入你要拷贝到的文件目录：");
        String DescPath = scanner.next();
        File file1 = new File(DescPath);
        if (file1.isFile()) {
            System.out.println("已经存在该文件了!");
            return;
        }
        try (InputStream inputStream = new FileInputStream(file);
             OutputStream outputStream = new FileOutputStream(file1)) {
            while(true){
                int ret = inputStream.read();
                if(ret == -1){
                    break;
                }
                outputStream.write(ret);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
